package dao;

import org.apache.ibatis.annotations.*;
import pojo.Wz;

import java.util.List;

@Mapper
/**
 * 文章表数据访问接口
 */
public interface WzMapper {
    // 根据id查询文章
    @Select("SELECT * FROM wz WHERE id = #{id}")
    Wz findWzById(Integer id);

    // 查询所有文章
    @Select("SELECT * FROM wz ORDER BY create_time DESC")
    List<Wz> findAllWz();

    // 按分类查询文章
    @Select("SELECT * FROM wz WHERE category = #{category} ORDER BY create_time DESC")
    List<Wz> findWzByCategory(String category);

    // 按访问量查询热门文章
    @Select("SELECT * FROM wz ORDER BY view_count DESC LIMIT #{limit}")
    List<Wz> findHotWz(@Param("limit") int limit);

    // 搜索文章
    @Select("SELECT * FROM wz WHERE " +
            "title LIKE CONCAT('%', #{keyword}, '%') OR " +
            "content LIKE CONCAT('%', #{keyword}, '%')")
    List<Wz> searchWz(String keyword);

    // 新增文章（使用数据库默认值）
    @Insert("INSERT INTO wz (title, content, category) " +
            "VALUES (#{title}, #{content}, #{category})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addWz(Wz wz);

    // 更新文章
    @Update("UPDATE wz SET " +
            "title = #{title}, " +
            "content = #{content}, " +
            "category = #{category} " +
            "WHERE id = #{id}")
    int updateWz(Wz wz);

    // 更新访问量
    @Update("UPDATE wz SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(Integer id);

    // 删除文章
    @Delete("DELETE FROM wz WHERE id = #{id}")
    int deleteWz(Integer id);
}